﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class StockPanel : MonoBehaviour
{
    public GameObject m_CompanyList;
    public StockItem m_StockItemPerfab;
    public List<StockItem> m_StockItems = new List<StockItem>();
    public Text m_AllPriceL;
    public Text m_MoneyL;
    public Text m_Description;
    public Text m_StockName;
    public GameObject m_History;
    public HistoryItem m_HistoryItemPerfab;
    public List<HistoryItem> m_HistoryItems = new List<HistoryItem>();
    float m_AllPrice = 0;
    private void Start()
    {
        foreach (var stock in StockFactory.Instance.m_Stockbjs)
        {
            StockItem item = Instantiate(m_StockItemPerfab);
            item.Stock = stock.CreateStock();
            item.transform.SetParent(m_CompanyList.transform);
            item.transform.localScale = new Vector3(1,1,1);
            item.ClickedCallback = OnItemClicked;
            m_StockItems.Add(item);
        }
    }

    private void Update()
    {
        
    }
    private void FixedUpdate()
    {
        m_MoneyL.text = "$"+Player.Instance.Money.ToString();
        m_AllPrice = 0;
        foreach(var item in m_StockItems)
        {
            m_AllPrice += item.m_PriceNow * item.OprationNumber;
        }
        if (m_AllPrice < 0)
        {
            m_AllPriceL.color = new Vector4(0, 1, 0, 1);
            m_AllPriceL.text = "+"+(-m_AllPrice).ToString("F2");
        }
        else
        {
            m_AllPriceL.color = new Vector4(1, 0, 0, 1);
            m_AllPriceL.text = (-m_AllPrice).ToString("F2");
        }
    }

    public void OnConfimClicked()
    {
        if(m_AllPrice > Player.Instance.Money)
        {
            MessageShow.Instance.Messaage("你钱不够~", new Vector4(1, 0, 0, 1));
            return;
        }

        Player.Instance.Money -= (int)m_AllPrice;

        foreach(var item in m_StockItems)
        {
            if(item.OprationNumber != 0)
            {
                Commander.Instance.AddStockNumber(item.Stock.m_ID.ToString(),item.OprationNumber);
            }
            item.OprationNumber = 0;
            item.UpdateInformation();
        }

        MessageShow.Instance.Messaage("交易成功~", new Vector4(0, 1, 0, 1));
        Player.Instance.save();
        Commander.Instance.save();


    }
    public void OnItemClicked(Stock stock)
    {
        m_StockName.text = stock.m_Name;
        m_Description.text = stock.m_Describe;

        if(m_HistoryItems.Count<60)
        {
            for (int i = 0; i < 60; ++i)
            {
                var historyItem = Instantiate(m_HistoryItemPerfab);
                historyItem.transform.SetParent(m_History.transform);
                historyItem.transform.localScale = new Vector3(1,1,1);
                m_HistoryItems.Add(historyItem);
            }
        }
        for (int i = 0; i < 60; ++i)
        {
            m_HistoryItems[i].Init(stock, i);
        }

    }
}
